/*************************************
 * Created by WebStorm
 * Author : 艾尼亚尔·毛拉吾提
 * QQ : 524155792  WeChat : ainiyar0124
 * Created Time :2024/2/5 18:35
 Desc :
 **************************************/
import {defineStore} from 'pinia';
import {UseAppStore} from '@/stores/app.ts';
import {setLocalStorage, getLocalStorage, removeLocalStorage} from '@/util'
import {ResultEnum} from '@/util/http/enums/httpEnum';
import {setToken, removeToken, setTokenTime, removeTokenTime} from '@/util/Auth.ts'
import {login,logout} from '@/api/user.ts';

interface UserState {
    userName: string;
    nickName: string;
    avatar: string;
}

export const UseUserStore = defineStore({
    id: "user",
    state: (): UserState => ({
        userName: getLocalStorage("userName"),
        nickName: getLocalStorage("nickName"),
        avatar: getLocalStorage("avatar") || "",
    }),
    getters: {},
    actions: {
        setUserName(name: string) {
            this.userName = name;
            setLocalStorage("userName", name);
        },
        setNickName(nickName: string) {
            this.nickName = nickName;
            setLocalStorage("nickName", nickName);
        },
        setAvatar(avatar: string) {
            this.avatar = avatar;
            setLocalStorage("avatar", avatar);
        },
        setToken(token: string, time: number) {
            setToken(token, time)
            setTokenTime(time);
        },
        setLogOut() {
            removeToken();
            removeLocalStorage("tabs_list");
            removeLocalStorage("userName");
            removeLocalStorage("nickName");
            removeLocalStorage("avatar");
            removeLocalStorage("menu_routers");
            removeLocalStorage("authorities");
            removeTokenTime();
            const appStore = UseAppStore();
            appStore.tabsList = [];
        },
        //系统登录
        async login(params: any) {
            try {
                const response = await login(params);
                if (response != undefined) {
                    const {data, code} = response;
                    if (code === ResultEnum.SUCCESS) {
                        this.setToken(data.token, data.expireTime);
                        this.setUserName(data.account);
                        this.setNickName(data.nickName?data.nickName:null);
                        this.setAvatar(data.avatar?data.avatar:null);
                        setLocalStorage("menu_routers", data.menus);
                        setLocalStorage("authorities", data.authorities)
                        setLocalStorage("last_login", {time: data.last_login, ip: data.last_ip});
                    }
                    return Promise.resolve(response);
                }
            } catch (e) {
                return Promise.reject(e);
            }
        },
        //系统退出登录
        async logOut() {
            try {
                const response = await logout();
                this.setLogOut();
                return Promise.resolve(response);
            } catch (e) {
                return Promise.reject(e);
            }
        },
        //重新登录 保留tabs
        async loginAgain(){
            try {
                const response = await logout();
                removeToken();
                removeLocalStorage("userName");
                removeLocalStorage("nickName");
                removeLocalStorage("avatar");
                removeLocalStorage("menu_routers");
                removeLocalStorage("authorities");
                removeTokenTime();
                return Promise.resolve(response);
            } catch (e) {
                return Promise.reject(e);
            }
        }
    },
});